package test.account;
// 交易流

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import hyl.base.flow.Flow;
import hyl.base.flow.Track;
import hyl.core.MyFun;


// 扩展了Flow类
public class TradeFlow {
	Flow _flow;
	TradeDao _fdao;
	// 缓存所有交易中的流水
	Map<String, Track> _track = new ConcurrentHashMap<>();

	public TradeFlow(Flow flow, TradeDao dao) {
		_flow = flow;
		_fdao = dao;
	}

	// 创建一串交易流程的足迹实例
	public Track createTrack() {
		String flowid = MyFun.getUUID();
		Track TR = _flow.getTrkInstance(flowid);
		_track.put(flowid, TR);
		return TR;
	}

	public void run乙方下订单(String flowid, String 操作员, String 甲方, String 乙方, String 备注, List<Object[]> 购物车明细) {
		// 获取当前工作流水的流水足迹
		Track track = _track.get(flowid);
		String link = "乙方下订单";
		track.setAttribute("甲方", 甲方);// 临时保存
		track.setAttribute("乙方", 乙方);// 临时保存
		_flow.getLink(link).run(track);
		Long linkid = track.linkids.get(link); // 获取足迹环节的编号
		_fdao.turnto交易清单(flowid, 购物车明细);
		// 汇总交易明细,添加到足迹环节附表
		_fdao.sum交易清单(flowid, 操作员, linkid, link + 备注);		
		// 批处理
		_fdao.commit();
	}

	public void run乙方付款(String flowid, String 操作员, BigDecimal 金额, String 备注) {
		Track track = _track.get(flowid);
		String link = "乙方付款";
		track.setAttribute("金额", 金额);// 临时保存
		String 甲方 = (String) track.getAttribute("甲方");// 临时保存
		String 乙方 = (String) track.getAttribute("乙方");// 临时保存
		BigDecimal je = 金额.negate();
		_flow.getLink(link).run(track);
		Long linkid = track.linkids.get(link);
		_fdao.insert环节_通用(linkid, 操作员, 甲方, 乙方, 金额, 备注);
		// 记账
		_fdao.insert账本(flowid, 乙方, "钱包", je, link + 备注);
		_fdao.insert账本(flowid, 乙方, "应付", 金额, link + 备注);
		_fdao.commit();
	}

	public void run甲方发货(String flowid, String 操作员, String 发货地址, Date 发货日期, String 收货地址, BigDecimal 快递费, Integer 总件数,
			String 发货凭证, String 备注) {
		Track track = _track.get(flowid);
		String link = "甲方发货";
		_flow.getLink(link).run(track);
		String 甲方 = (String) track.getAttribute("甲方");// 临时保存
		String 乙方 = (String) track.getAttribute("乙方");// 临时保存
		Long linkid = track.linkids.get(link);
		_fdao.insert环节_发货(linkid, 操作员, 甲方, 乙方, 发货地址, 发货日期, 收货地址, 快递费, 总件数, 备注, 发货凭证);
		BigDecimal 金额 = (BigDecimal) track.getAttribute("金额");
		BigDecimal je = 金额.negate();
		// 记账
		_fdao.insert账本(flowid, 甲方, "库存", je, link + 备注);
		_fdao.insert账本(flowid, 甲方, "在途", 金额, link + 备注);
		_fdao.commit();
	}

	public void run乙方签收(String flowid) {
		Track track = _track.get(flowid);
		_flow.getLink("乙方签收").run(track);
	}

	public void run乙方退货(String flowid) {
		Track track = _track.get(flowid);
		_flow.getLink("乙方退货").run(track);
	}

	public void run甲方回收(String flowid) {
		Track track = _track.get(flowid);
		String link = "甲方回收";
		_flow.getLink(link).run(track);
		String 甲方 = (String) track.getAttribute("甲方");// 临时保存
		BigDecimal 金额 = (BigDecimal) track.getAttribute("金额");
		BigDecimal je = 金额.negate();
		_fdao.insert账本(flowid, 甲方, "在途", je, link);
		_fdao.insert账本(flowid, 甲方, "库存", 金额, link);
		_fdao.commit();
	}

	public void run甲方退款(String flowid) {
		Track track = _track.get(flowid);
		_flow.getLink("甲方退款").run(track);
	}

	public void run乙方收款(String flowid) {
		Track track = _track.get(flowid);
		String link = "乙方收款";
		_flow.getLink(link).run(track);
		String 甲方 = (String) track.getAttribute("甲方");// 临时保存
		String 乙方 = (String) track.getAttribute("乙方");// 临时保存
		BigDecimal 金额 = (BigDecimal) track.getAttribute("金额");
		BigDecimal je = 金额.negate();
		// Long linkid= track.linkids.get("乙方收款");
		// _fdao.insertLink2(linkid, 操作员, 甲方, 乙方, 金额, "收甲方退款");

		_fdao.insert账本(flowid, 乙方, "应付", je, link);
		_fdao.insert账本(flowid, 乙方, "钱包", 金额, link);
		_fdao.commit();
	}

	public void run乙方确认付款(String flowid) {
		Track track = _track.get(flowid);
		_flow.getLink("乙方确认付款").run(track);
		String 甲方 = (String) track.getAttribute("甲方");
		String 乙方 = (String) track.getAttribute("乙方");// 临时保存
		BigDecimal 金额 = (BigDecimal) track.getAttribute("金额");
		BigDecimal je = 金额.negate();
		// Long linkid= track.linkids.get("乙方收款");
		// _fdao.insertLink2(linkid, 乙方, 甲方, 乙方, 金额, "乙方确认付款");
		String 备注 = "乙方确认付款";
		_fdao.insert账本(flowid, 乙方, "应付", je, 备注);
		_fdao.insert账本(flowid, 乙方, "库存", 金额, 备注);
		_fdao.insert账本(flowid, 甲方, "在途", je, 备注);
		_fdao.insert账本(flowid, 甲方, "钱包", 金额, 备注);
		_fdao.commit();
	}

	public void run甲方归档(String flowid) {
		Track track = _track.get(flowid);
		_flow.getLink("甲方归档").run(track);
		_track.remove(flowid);
	}

}
